package com.permission.systempermission.comm.security;

import com.alibaba.fastjson.JSON;
import com.permission.systempermission.comm.Result.ResultVo;
import com.permission.systempermission.comm.constant.CheckResult;
import com.permission.systempermission.utils.JwtUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginNoAuthentication implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        String token = httpServletRequest.getHeader("token");
        String json = "";
        CheckResult checkResult = JwtUtils.validateJWT(token);
        if (!checkResult.isSuccess()) {
            json = JSON.toJSONString(ResultVo.error("401 未经授权"));
            httpServletResponse.getWriter().println(json);
        }
    }

}
